#include "stdafx.h"
#include "ui_fontbox.h"

#include <qpixmap.h>
#include <qstringlist.h>

#include "pf_debug.h"


/**
 * Default Constructor. You must call init manually if you choose
 * to use this constructor.
 */
UI_FontBox::UI_FontBox(QWidget* parent, const char* name)
        : QComboBox(parent, name) {}



/**
 * Destructor
 */
UI_FontBox::~UI_FontBox() {}


/**
 * Initialisation (called from constructor or manually but only
 * once).
 */
void UI_FontBox::init() {
    QStringList fonts;

    QListIterator<PF_Font *> i = PF_FONTLIST->getIteretor();
    while (i.hasNext()) {
        fonts.append( i.next()->getFileName() );
    }

    fonts.sort();
    insertStringList(fonts);

    connect(this, SIGNAL(activated(int)),
            this, SLOT(slotFontChanged(int)));

    setCurrentItem(0);
    slotFontChanged(currentItem());
}



/**
 * Sets the currently selected width item to the given width.
 */
void UI_FontBox::setFont(const QString& fName) {

    PF_DEBUG->print("UI_FontBox::setFont %s\n", fName.toLatin1().data());

    setCurrentText(fName);

    slotFontChanged(currentItem());
}



/**
 * Called when the font has changed. This method 
 * sets the current font to the value chosen.
 */
void UI_FontBox::slotFontChanged(int index) {

    PF_DEBUG->print("UI_FontBox::slotFontChanged %d\n", index);

    currentFont = PF_FONTLIST->requestFont(currentText());

    if (currentFont!=NULL) {
        PF_DEBUG->print("Current font is (%d): %s\n",
                        index, currentFont->getFileName().toLatin1().data());
    }

    emit fontChanged(currentFont);
}


